package com.itheima.health.controller;

import com.aliyuncs.utils.StringUtils;
import com.github.pagehelper.util.StringUtil;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.util.SMSUtils;
import com.itheima.health.util.ValidateCodeUtils;
import com.sun.prism.impl.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/mobile/validateCode")
@Slf4j
public class ValidateCodeController {
    @Autowired
    private RedisTemplate redisTemplate;
//发送验证码
    @PostMapping("/send")
    public Result send(String type, String telephone) {

        if (StringUtil.isNotEmpty(telephone)){//手机号不为空
            //调用阿里云发送
//            SMSUtils.sendMessage("传智健康","",telephone,code);
            String  code = ValidateCodeUtils.generateValidateCode(4).toString();

            log.info("我们要发送给用户的验证码是:" + code);
            redisTemplate.opsForValue().set(telephone,code,5,TimeUnit.MINUTES);
            return new Result(true, MessageConst.LOGIN_SUCCESS);
        }
        return new Result(false,MessageConst.LOGIN_FAIL);
    }

}